from fastapi import APIRouter, Depends, HTTPException, Request, Form
from sqlalchemy.orm import Session
from app.schemas.report import OrderDailyReportSchema
from app.utils.resp_util import APIResponse
from datetime import datetime, timedelta

from app.extensions.dependencies import get_query_token2
from app.dependencies.db import get_db

from app import services,  utils

import logging

logger = logging.getLogger(__name__)

router = APIRouter(prefix='/trans', tags=['trans_report'])

@router.get("/gen_daily_trans_reports", response_model=APIResponse[list[str]])
async def gen_daily_trans_reports(start: str=None, end: str=None, db: Session = Depends(get_db)):
    logger.debug('>> gen_daily_trans_reports')
    if start:
        start_dt = utils.time.str_to_time(start, 5)
    else:
        start_dt = utils.time.get_date_zero_time()
    if end:
        end_dt = utils.time.str_to_time(end, 5)
    else:
        end_dt = utils.time.get_next_day_zero_time()
    results = services.report.trans.gen_and_save_daily_trans_reports(db, start_dt, end_dt)
    resp = APIResponse()
    resp.data = results
    return resp
    #resp = utils.job.call_job('trans.trace_order_status', jobs.JOBS_DICT, args=[order_no])
    #return resp

@router.get("/get_trans_report_data", response_model=APIResponse[list[OrderDailyReportSchema]])
async def get_to_be_send_notices(days: int=None, start: str=None, end: str=None, db: Session = Depends(get_db)):
    logger.debug('>> get_trans_report_data')
    results = services.report.trans.get_trans_report_data(db, days=days, start=start, end=end)
    resp = APIResponse()
    resp.data = results
    return resp
